package com.fyp.speachrecognition;

import java.util.List;
import java.util.StringTokenizer;

import com.fyp.database.DataProvider;
import com.fyp.database.User;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class ViewAccounts extends ListActivity{
	private DataProvider dp;
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.viewcontact);
	    Button del = (Button) findViewById(R.id.delete);
	    dp = new DataProvider(this);
	    dp.open();
	    
	    List<User> values = dp.getAllUsers();
	    
	    if(values!=null){
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
	    for(int i=0;i<values.size();i++)
	    adapter.add("User Name: "+values.get(i).getUserName()+"\nPassword: "+values.get(i).getPassword());
	    Log.v("View Values: ",values.get(0).getUserName());
	        setListAdapter(adapter);}
	    
	    del.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
			    String u = null;
			    if (getListAdapter().getCount() > 0) {
			        u = (String) getListAdapter().getItem(0);
			        String r=u;
			        String user=null,pass=null;
			        StringTokenizer st = new StringTokenizer(u,":\n");
			        while(st.hasMoreTokens())
			        {
			        	u=st.nextToken();
			        	if(u.equals("User Name"))
			        	{
			        		u=st.nextToken();
			        		u=u.trim();
			        		user=u;
			        		Log.v("User: ",u);
			        	}
			        	else if(u.equals("Password"))
			        	{
			        		u=st.nextToken();
			        		u=u.trim();
			        		pass=u;
			        		Log.v("Pass: ",u);
			        		break;
			        	}
			        }
			        if(user!=null&&pass!=null){
			        	dp.deleteUser(user,pass);
			        	adapter.remove(r);}
			      }
			    adapter.notifyDataSetChanged();
			}
		});
	};
	 // Will be called via the onClick attribute
	  // of the buttons in main.xml
	

	  @Override
	  protected void onResume() {
	    dp.open();
	    super.onResume();
	  }

	  @Override
	  protected void onPause() {
	    dp.close();
	    super.onPause();
	  }

	 
}
